package com.aaa.hospital.Client.controller;

import com.aaa.hospital.Client.pojo.Department;
import com.aaa.hospital.Client.pojo.Registration;
import com.aaa.hospital.Client.pojo.User;
import com.aaa.hospital.Client.pojo.Visitingcard;
import com.aaa.hospital.Client.service.*;
import com.aaa.hospital.Client.util.DocuserDB;
import com.aaa.hospital.Client.util.UserDB;
import com.aaa.hospital.ManagementEnd.pojo.Docuser;
import com.aaa.hospital.ManagementEnd.pojo.Registration2;
import com.aaa.hospital.ManagementEnd.service.RegistrationServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//徐要磊

@Controller
public class SubscribeController {
    @Autowired
    private UserServicetwo userServicetwo;
    @Autowired
    private RegistrationService registrationService;
    @Autowired
    private DoctorinformationService doctorinformationService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private Visitingcard_xyService visitingcard_xyService;
    @Autowired
    private VisitingcardService visitingcardService;
    @Autowired
    private RegistrationServices registrationServices;



//提交预约信息
    @RequestMapping("/subscribe")
    @ResponseBody
    public  Integer subscribe(UserDB userDB, Registration registration){
        System.out.println("预约的信息"+userDB);

        List<User> a=userServicetwo.queryName(userDB.getIdcard());

        if(a.size()==0){
//            userServicetwo.inserttwo(userDB);
//            a=userServicetwo.queryName(userDB.getName());
//            visitingcardService.insertBalance(0,a.get(0).getUid());
//
//            List<Visitingcard> list4= visitingcard_xyService.queryByName(a.get(0).getUid());
//
//
//            Registration list= new Registration(null,list4.get(0).getVisid(), userDB.getDid()·, userDB.getKsid(), userDB.getBooking_date(),new Date(),userDB.getSchedule(),null);
//
//            registrationService.insert(list);
            return 0;
        }else{

            List<Visitingcard> list4= visitingcard_xyService.queryByName(a.get(0).getUid());
            List<Registration2> list5= registrationServices.queryBySize(list4.get(0).getVisid(),userDB.getBooking_date());
            if(list5.size()==0){
//                Registration list= new Registration(null,list4.get(0).getVisid(), userDB.getDid(), userDB.getKsid(), userDB.getBooking_date(),new Date(),userDB.getSchedule(),null);

//                registrationService.insert(list);
                return 1;
            }else{
                return 3;
            }
        }
    }



    @RequestMapping("/subscribe2")
    @ResponseBody
    public  Integer subscribe2(UserDB userDB, Registration registration){
        System.out.println("预约的信息"+userDB);

        List<User> a=userServicetwo.queryName(userDB.getIdcard());

        if(a.size()==0){
            userServicetwo.inserttwo(userDB);
            a=userServicetwo.queryName(userDB.getIdcard());
            visitingcardService.insertBalance(0,a.get(0).getUid());
            List<Visitingcard> list4= visitingcard_xyService.queryByName(a.get(0).getUid());
            Registration list= new Registration(null,list4.get(0).getVisid(), userDB.getDid(), userDB.getKsid(), userDB.getBooking_date(),new Date(),userDB.getSchedule(),null);
            registrationService.insert(list);
            return list4.get(0).getVisid();
        }else{
            List<Visitingcard> list4= visitingcard_xyService.queryByName(a.get(0).getUid());
            List<Registration2> list5= registrationServices.queryBySize(list4.get(0).getVisid(),userDB.getBooking_date());
            if(list5.size()==0){
                Registration list= new Registration(null,list4.get(0).getVisid(), userDB.getDid(), userDB.getKsid(), userDB.getBooking_date(),new Date(),userDB.getSchedule(),null);
                System.out.println(list);
                registrationService.insert(list);
                return 1;
            }else{
                return 3;
            }
        }
    }





//获取到医生信息
    @RequestMapping("/subdocuser")
    @ResponseBody
    public List subdocuser(String date1,int id,DocuserDB docuserDB){
        List<Docuser> list=doctorinformationService.queryAll(id);
        DocuserDB docuserDB1=null;
        List list2=new ArrayList();
        List list3=new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            List<Docuser>list1 = doctorinformationService.querysum(date1,list.get(i).getDid());
//            list2.add(list.get(i));
            docuserDB1=new DocuserDB(list1.size(),list.get(i));
            list3.add(docuserDB1);
        }


        return list3;
    }
    //获取科室信息
    @RequestMapping("/subdeparement")
    @ResponseBody
    public List subdeparement(){

        List<Department> list = departmentService.queryAll();

        return list;
    }

    //获取用户信息
    @RequestMapping("/logindate")
    @ResponseBody
    public List logindate(HttpSession session){
        if(session.getAttribute("user")!=null){
            String username= (String) session.getAttribute("user");
            Integer a=Integer.parseInt(username);
//            List<User> list=userServicetwo.queryByName(username);
            List<User> list=userServicetwo.queryByName2(a);
            return list;
        }else{
            return null;
        }

    }


    @RequestMapping("/loginout5")
    @ResponseBody
    public String loginout3(HttpSession session){
            String a= (String) session.getAttribute("username");

            return a;

    }


}
